home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / qlib205.zip / QLIB.ZIP / C / PASM.H < prev    next >
Text File  |  1997-01-14  |  10KB  |  406 lines

  1. //out2nd = //output to ho2 (string file)
  2. //out = //output to ho1  (ASM file)
  3.  
  4. void out(void);
  5. void out2nd(void);
  6.  
  7. //pre-assemble file
  8. void prefile(void) {
  9.   word pb,p1,p2,p3,ttt;
  10.   byte a,b,c;
  11.   byte f1;  //misc flags
  12.   byte f2;
  13.   byte f3;
  14.   byte f4;
  15.   byte comment,commentchar;
  16.   comment=0;
  17.   while (!EOFF[ch]) {
  18.     readln();
  19.     strcpy(str,buf);
  20.     pb=0;
  21.     if (comment) {
  22.       while (*(buf+pb)) {
  23.         if (*(buf+pb)==commentchar) {
  24.           comment=0;
  25.           break;
  26.         }
  27.         pb++;
  28.       }
  29.       continue;
  30.     }
  31.     //get 1st word (looking for include/#include/strings"")
  32.     pb=0;
  33.     while (buf[pb]) {
  34.       a=buf[pb];
  35.       if (a==32) {pb++;continue;}
  36.       if (a==' ') {pb++;continue;}
  37.       break;
  38.     }
  39.     if (!memcmp(buf+pb,"include",7)) {
  40.       //include found
  41.       //this line is lost!
  42.       pb+=7;
  43.       buf[pb]=0;
  44.       while (!buf[pb]) {      //NEW in v1.1
  45.         pb++;
  46.         if (buf[pb]==32) {buf[pb]=0;continue;}
  47.         if (buf[pb]==';') {buf[pb]=0;continue;}
  48.       }
  49.       ttt=pb;
  50.       while (buf[pb]) {
  51.         pb++;
  52.         if (buf[pb]==32) {buf[pb]=0;break;}
  53.       }
  54.       pb=ttt;
  55.       file=buf+pb;
  56.       dofile();  //recursive call
  57.       continue;
  58.     }
  59.     if (!memcmp(buf+pb,"comment ",8)) {
  60.       comment=1;
  61.       pb+=7;
  62.       while (*(buf+pb)==32) {
  63.         pb++;
  64.         if (!*(buf+pb)) errorf("Invalid comment directive");
  65.       }
  66.       commentchar=*(buf+pb);
  67.       continue;
  68.     }
  69.     if (!memcmp(buf+pb,"#include",8)) {
  70.       str[pb]=32;  //remove the # and move on
  71.       foundit=1;
  72.     }
  73.     pb=0;
  74.     f1=0;  // '' string in use
  75.     f2=0;  // "" string in use
  76.     f3=0;  // proper '' found!
  77.     f4=0;  // proper "" found!
  78.     while (buf[pb]) {
  79.       // 39 = ''''
  80.       if (f1) {  //'
  81.         if ((buf[pb]=='\'')&&(buf[pb+1]=='\'')) {
  82.           pb+=2;
  83.           continue;
  84.         }
  85.         if (buf[pb]=='\'') {
  86.           f1=0;
  87.           f3=1;
  88.         }
  89.         pb++;
  90.         continue;
  91.       }
  92.       if (f2) { //"
  93.         if ((buf[pb]=='"')&&(buf[pb+1]=='"')) {
  94.           pb+=2;
  95.           continue;
  96.         }
  97.         if ((buf[pb]=='\\')&&(buf[pb+1]=='\\')) {   //NEW in v1.1
  98.           pb+=2;
  99.           continue;
  100.         }
  101.         if ((buf[pb]=='\\')&&(buf[pb+1]=='"')) {   //NEW in v1.1
  102.           pb+=2;
  103.           continue;
  104.         }
  105.         if (buf[pb]=='"') {
  106.           f2=0;
  107.           f4=1;
  108.         }
  109.         pb++;
  110.         continue;
  111.       }
  112.       if (buf[pb]=='"') f2=1;
  113.       if (buf[pb]=='\'') f1=1;
  114.       if (buf[pb]==';') break;  //ignore rest of line
  115.       pb++;
  116.     }
  117.     buf[pb]=0;   //in case we remove the REMed part
  118.     if ((f2)||(f1)) {
  119.       errorf("string left open[1]\r\n");
  120.     }
  121. // was removed in version 1.02
  122. //    if ((f3)&&(f4)) errorf("not allowed to mix strings on a line\r\n");
  123.     f1=0;  //start of new string
  124.     f2=0;  //just added \r\n (13,10) or \0  (0)
  125.     // BUG!!?
  126.     f3=0;  //in mid of string
  127.     if (f4) {
  128.       //string found
  129.       pb=0; //buf    invoke printf,"SEGA!\r\n"
  130.       p1=0; //str    invoke printf,offset str_0000
  131.       p2=0; //str2   'SEGA!',13,10
  132.       str[0]=0;
  133. //      str2[0]=0;  //not needed
  134.       while (buf[pb]) {
  135. #ifdef DEBUG2
  136.           printf("%i ",pb);
  137. #endif
  138.         if (buf[pb]=='"') {
  139.           //handle string
  140.           f1=1;
  141.           f2=0;
  142.           f3=0;
  143.           pb++;
  144.           p2=0;
  145.           str2[0]=0;
  146.           while (1) {
  147.             if ( (buf[pb]=='"') && (buf[pb+1]=='"')) {
  148.               if (f1) {
  149.                 f1=0;
  150.                 str2[p2++]='\'';
  151.               }
  152.               if (f2) {
  153.                 f2=0;
  154.                 str2[p2++]=',';
  155.                 str2[p2++]='\'';
  156.               }
  157.               pb++;
  158.               f3=1;
  159.               str2[p2++]=buf[pb++];
  160.               continue;
  161.             }
  162.             if (buf[pb]=='"') break;
  163.             if (buf[pb]==0) errorf("string left open[2]\r\n");
  164.             if ((buf[pb]=='\\') && (buf[pb+1]=='\"')) {
  165.               if (f1) f1=0;
  166.               pb+=2;
  167.               if (f3) {
  168.                 str2[p2++]='\'';
  169.                 f3=0;
  170.                 str2[p2]=0;
  171.                 strcat(str2,",34");
  172.                 p2+=3;
  173.                 f2=1;
  174.                 continue;
  175.               }
  176.               if (f2) str2[p2++]=','; else f2=1;
  177.               str2[p2]=0;
  178.               strcat(str2,"34");
  179.               p2+=2;
  180.               continue;
  181.             }
  182.             if ((buf[pb]=='\\') && (buf[pb+1]=='\'')) {
  183.               if (f1) f1=0;
  184.               pb+=2;
  185.               if (f3) {
  186.                 str2[p2++]='\'';
  187.                 f3=0;
  188.                 str2[p2]=0;
  189.                 strcat(str2,",39");
  190.                 p2+=3;
  191.                 f2=1;
  192.                 continue;
  193.               }
  194.               if (f2) str2[p2++]=','; else f2=1;
  195.               str2[p2]=0;
  196.               strcat(str2,"39");
  197.               p2+=2;
  198.               continue;
  199.             }
  200.             if ((buf[pb]=='\\') && (buf[pb+1]=='t')) {   //NEW : v1.11
  201.               if (f1) f1=0;
  202.               pb+=2;
  203.               if (f3) {
  204.                 str2[p2++]='\'';
  205.                 f3=0;
  206.                 str2[p2]=0;
  207.                 strcat(str2,",09");
  208.                 p2+=3;
  209.                 f2=1;
  210.                 continue;
  211.               }
  212.               if (f2) str2[p2++]=','; else f2=1;
  213.               str2[p2]=0;
  214.               strcat(str2,"09");
  215.               p2+=2;
  216.               continue;
  217.             }
  218.             if ((buf[pb]=='\\') && (buf[pb+1]=='n')) {
  219.               if (f1) f1=0;
  220.               pb+=2;
  221.               if (f3) {
  222.                 str2[p2++]='\'';
  223.                 f3=0;
  224.                 str2[p2]=0;
  225.                 strcat(str2,",10");
  226.                 p2+=3;
  227.                 f2=1;
  228.                 continue;
  229.               }
  230.               if (f2) str2[p2++]=','; else f2=1;
  231.               str2[p2]=0;
  232.               strcat(str2,"10");
  233.               p2+=2;
  234.               continue;
  235.             }
  236.             if ((buf[pb]=='\\') && (buf[pb+1]=='r')) {
  237.               if (f1) f1=0;
  238.               pb+=2;
  239.               if (f3) {
  240.                 str2[p2++]='\'';
  241.                 f3=0;
  242.                 str2[p2]=0;
  243.                 strcat(str2,",13");
  244.                 p2+=3;
  245.                 f2=1;
  246.                 continue;
  247.               }
  248.               if (f2) str2[p2++]=','; else f2=1;
  249.               str2[p2]=0;
  250.               strcat(str2,"13");
  251.               p2+=2;
  252.               continue;
  253.             }
  254.             if ((buf[pb]=='\\') && (buf[pb+1]=='0')) {
  255.               if (f1) f1=0;
  256.               pb+=2;
  257.               if (f3) {
  258.                 str2[p2++]='\'';
  259.                 f3=0;
  260.                 str2[p2]=0;
  261.                 strcat(str2,",0");
  262.                 p2+=2;
  263.                 f2=1;
  264.                 continue;
  265.               }
  266.               if (f2) str2[p2++]=','; else f2=1;
  267.               str2[p2]=0;
  268.               strcat(str2,"0");
  269.               p2+=1;
  270.               continue;
  271.             }
  272.             if ((buf[pb]=='\\') && (buf[pb+1]=='\\')) {
  273.               if (f1) {
  274.                 f1=0;
  275.                 str2[p2++]='\'';
  276.                 f3=1;
  277.               }
  278.               if (!f3) {
  279.                 if (f2) {str2[p2++]=',';f2=0;}
  280.                 str2[p2++]='\'';
  281.                 f3=1;
  282.               }
  283.               pb++;
  284.               str2[p2++]=buf[pb++];
  285.               continue;
  286.             }
  287.             if (f1) {
  288.               f1=0;
  289.               str2[p2++]='\'';
  290.             }
  291.             if (f2) {
  292.               f2=0;
  293.               str2[p2++]=',';
  294.               str2[p2++]='\'';
  295.             }
  296.             f3=1;
  297.             if (buf[pb]=='\'') str2[p2++]='\'';    // FIX : v1.2
  298.             str2[p2++]=buf[pb++];
  299.           } //end while
  300.           if (f1) {       //null string  FIX v1.1
  301.             str2[p2++]='0';
  302.             str2[p2++]=0;
  303.             anulstr=1;
  304.           } else {
  305.             if (f2) str2[p2]=0;
  306.             else {
  307.               str2[p2++]='\'';
  308.               str2[p2]=0;
  309.             }
  310.             anulstr=0;
  311.           }
  312.           out2nd();  //out the string stuff
  313.           pb++;  //pts now to next , (or end)
  314.           //finish up 1 string thingy
  315.           str[p1]=0;
  316. #ifdef DEBUG2
  317.           printf("%i\n",pb);
  318.           printf("%s\n",buf);
  319.           printf("%s\n\n",str);
  320. #endif
  321.           sprintf(str3,"offset str_%05u",cnum-1);
  322.           str[p1]=0;
  323.           strcat(str,str3);
  324.           p1=strlen(str);
  325. #ifdef DEBUG2
  326.           printf("%i\n",pb);
  327.           printf("%s\n",buf);
  328.           printf("%s\n\n",str);
  329. #endif
  330.         } else {
  331.           str[p1++]=buf[pb++];
  332.           str[p1]=0;  //not really needed (for debugging)
  333.         }
  334.       } //end while
  335.       str[p1]=0;
  336. #ifdef DEBUG2
  337.           printf("%i\n",pb);
  338.           printf("%s\n",buf);
  339.           printf("%s\n\n",str);
  340. #endif
  341.     } //end if "string mode"
  342.     out();  //output normal string  (may be mod by string stuff!!)
  343.   }
  344. }
  345.  
  346. void out(void) {
  347.   word a;
  348.   strcat(str,enter);
  349.   a=strlen(str);
  350.   if ((a+bufp1)>bufsiz) {
  351.     write(ho1,bufo1,bufp1);
  352.     bufp1=0;
  353.   }
  354.   memcpy(bufo1+bufp1,str,a);
  355.   bufp1+=a;
  356. }
  357.  
  358. //places str2 into ho2
  359.  
  360. void out2nd(void) {
  361.   word a;
  362.   word p;
  363.   p=strlen(str2);
  364.   if (!anulstr) str2[p++]=',';
  365.   str2[p++]='0';
  366.   str2[p]=0;
  367.   sprintf(str3,"str_%05u db ",cnum++);
  368.   if (cnum==64000) error("too many strings\r\n");  //unlikely!!!
  369.   strcat(str3,str2);
  370.   strcat(str3,enter);
  371.   a=strlen(str3);
  372.   if ((a+bufp2)>bufsiz) {
  373.     write(ho2,bufo2,bufp2);
  374.     bufp2=0;
  375.   }
  376.   memcpy(bufo2+bufp2,str3,a);
  377.   bufp2+=a;
  378. }
  379.  
  380. byte out2setup[]=".data\r\n";
  381.  
  382. void setup(void) {
  383.   //open files and setup out2nd
  384.   ho1=open("asm.tmp",O_BINARY|O_WRONLY|O_CREAT|O_TRUNC);
  385.   if (ho1==-1) error("creating asm.tmp\r\n");
  386.   ho1on=1;
  387.   ho2=open("_str_.tmp",O_BINARY|O_WRONLY|O_CREAT|O_TRUNC);
  388.   if (ho2==-1) error("creating _str_.tmp\r\n");
  389.   write(ho2,out2setup,7);
  390.   inc=(void*)getenv("INCLUDE");
  391.   if ((dword)inc==ERROR) {
  392.     printf("Warning:INCLUDE not set in enviroment\r\n");
  393.   } else {
  394.     incsiz=strlen(inc);
  395.   }
  396. }           
  397.  
  398. void flush(void) {
  399.   write(ho1,bufo1,bufp1);
  400.   write(ho2,bufo2,bufp2);
  401.   bufp1=0;
  402.   close(ho1);
  403.   ho1on=0;
  404.   close(ho2);
  405. }
  406.